import util from "../../common/js/util.js";
var app = getApp();
var base_width = 60;
var base_height = 39;
Component({
  mixins: [],
  data: {
    labelList1: [
      { text: "预计使用时间", name: "TIME5" },
      { text: "品名", name: "MATERIAL_NAME" },
      { text: "固定资产", name: "FIXED_ASSETS" },
      { text: "规格", name: "MODEL" },
      { text: "数量", name: "NUM" },
      { text: "单位", name: "METERING_NAME" },
      { text: "品牌要求", name: "BRAND" },
    ],
    dataList1: [
      {
        TIME5: "2022-03-29",
        MATERIAL_NAME: "镀锌铁丝",
        FIXED_ASSETS: "否",
        MODEL: "22#",
        NOTE: "",
        NUM: "6",
        METERING_NAME: "/",
        BRAND: "无"
      },
      {
        TIME5: "2022-03-29",
        MATERIAL_NAME: "铁棒",
        FIXED_ASSETS: "否",
        MODEL: "22#",
        NOTE: "",
        NUM: "6",
        METERING_NAME: "/",
        BRAND: "无"
      }
    ],
    list: [{ label: "是", value: 1 }, { label: "否", value: 0 }],
    // labelList2: [
    //   { text: "供应商1号"}
    // ],
    allcheck: false,
    tableHeight: 0,
    tableWidth1: 0,
    tableWidth2: 0,
    scrollY2_1: 0,
    scrollY2_2: 0,
    base_width,
    row_width: base_width,
    left: 0,
    right: 0,
    position_right: 0,
    disMove: false,
    open_btn_group: false,
    move: true,
    have_flex: false,//表格列中是否有固定项
    flex_num: 0,//固定项的数量
    flex_arr: [],
    select: "none",//none  不开启勾选  single 单选 multiple 多选
    show_i: true,//purchase_Pact 来自合同的表格不显示小i
    operator_btn_show: false,//是否显示操作按钮
    delete_btn_show: false,//是否显示删除按钮，如果有操作又有删除，优先显示操作

  },
  props: {
    labelList: Array,
    dataList: Array,
    show_i: true,//purchase_Pact 来自合同的表格不显示小i,或可以控制不显示
    operator_btn_show: false,//是否显示操作按钮
    delete_btn_show: false,//是否显示删除按钮，如果有操作又有删除，优先显示操作
    open_btn_group: false,//是否显示按钮操作区
    select: "none",//none  不开启勾选  single 单选 multiple 多选
    type: "",//purchase_Pact 来自合同的表格不显示小i
    onOperator: function onOperator(e) { },
    onDelete: function onDelete(e) { }
  },
  onInit() {
    this.loadCfig();

  },

  didMount() {
    this.setData({
      dataList1: this.props.dataList
    })
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    onTouchStart() {
      this.setData({
        disMove: true,
        move: false
      })
      //   console.log("--触摸开始");

    },
    onTouchEnd() {
      this.setData({
        disMove: false,
        move: true
      })
      //   console.log("--触摸结束");

    },
    tableItemClick(e) {
      let value = e.currentTarget.dataset.value;
      let othertext=e.currentTarget.dataset.othertext;
      if(util.isNotEmpty(othertext)){
        value+=othertext;
      }
      dd.alert({
        content: value
      })
    },
    onOperator(e) {
      let item = e.currentTarget.dataset.item;
      let dataIndex = e.currentTarget.dataset.dataIndex;
      item.dataIndex = dataIndex;
      this.props.onOperator(item)
    },
    onDelete(e) {
      let item = e.currentTarget.dataset.item;
      let dataIndex = e.currentTarget.dataset.dataIndex;
      item.dataIndex = dataIndex;
      // this.props.onDelete(item)
      let that = this;
      let list = this.data.dataList1;
      let tableHeight = this.data.tableHeight;
      tableHeight = Math.floor(tableHeight - base_height);
      dd.confirm({
        title: '温馨提示',
        content: '是否确认删除该品名?',
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        success: (result) => {
          if (result.confirm) {
            list.splice(dataIndex, 1);
            that.setData({
              'dataList1': list,
              tableHeight
            })
          }
        },
      });
    },
    loadCfig(obj) {
      // console.log(app.prototype.screenHeight);
      // console.log(app.prototype.windowWidth);
      let row_width = 0;
      let tableWidth1 = 0;

      let open_btn_group = this.props.open_btn_group;
      let labelList1 = this.props.labelList;
      let select = this.props.select;

      if (obj) {
        if (obj.labelList) {
          labelList1 = obj.labelList;
        }
        if (obj.select) {
          select = obj.select
        }
        if (obj.open_btn_group != undefined) {
          open_btn_group = obj.open_btn_group
        }
        if (obj.searchQuery != undefined) {
          this.setData({
            searchQuery: obj.searchQuery
          })
        }
      }

      let have_flex = false;//表格列中是否有固定项
      let flex_num = 0;//固定项的数量
      let flex_arr = [];
      for (let a = 0; a < labelList1.length; a++) {
        let item = labelList1[a];
        if (item.flex) {
          have_flex = true;
          flex_num++;
          flex_arr.push(item);
        }
      }

      let length = labelList1.length;
      if (have_flex) {
        length = length - flex_num;
      }
      // 开启勾选 加一格
      if (this.props.select != 'none') {
        //  length++;
      }

      if (open_btn_group) {
        // 如果开启 显示按钮区就 再加一格占位
        // length++;
      }
      // debugger
      if (length >= 6) {
        row_width = base_width;
        // tableWidth1 = (row_width + 1) * length;
        tableWidth1 = (row_width) * length;
      } else {
        //表格列小于6时 进行平铺

        let windowWidth = app.prototype.windowWidth;
        // 开启勾选 减去勾选框的宽度
      if (this.props.select != 'none') {
        windowWidth=windowWidth-49;
      }
        tableWidth1 = Math.round((windowWidth));

        if (open_btn_group) {
          length++;
        }
        row_width = Math.round(windowWidth / length) - 1;
        
        if(row_width<base_width){
          row_width=base_width;
        }

        let countWidth=0;
        for (let a = 0; a < labelList1.length; a++) {
          let item = labelList1[a];
          if(item.width){
            countWidth+=item.width;
          }else{
            countWidth+=base_width;
          }
        }
        if(tableWidth1<countWidth){
          tableWidth1=countWidth
        }
  //      tableWidth1 = (row_width) * length;
         
        if (open_btn_group&&labelList1.length<5) {
          tableWidth1 = tableWidth1 - row_width - 1;
        }
        tableWidth1 = tableWidth1 - length;
      }

      let show_i = this.props.show_i;;
      if (this.props.type) {
        if (this.props.type == "purchase_Pact") {
          show_i = false;
        }
      } 
      let operator_btn_show = this.props.operator_btn_show;
      let delete_btn_show = false;

      if (this.props.delete_btn_show != undefined) {
        delete_btn_show = this.props.delete_btn_show
      }
      if (util.isNotEmpty(obj)) {
        if (obj.operator_btn_show != undefined) {
          operator_btn_show = obj.operator_btn_show;
        }
        if (obj.delete_btn_show != undefined) {
          delete_btn_show = obj.delete_btn_show;
        }
        if(obj.show_i!=undefined){
          show_i=obj.show_i
        }
      }
    //  debugger
      this.setData({
        flex_arr,
        have_flex,
        flex_num,
        show_i,
        select,
        row_width,
        open_btn_group,
        delete_btn_show,
        operator_btn_show,
        tableHeight: (this.props.dataList.length) * base_height - 6,
        tableWidth1,
        labelList1,
      });


    },
    initData(list) {
      //let select = this.props.select;
      let labelList = this.data.labelList1;

      list.forEach(element => {
        if(this.data.select!='none'){
          element.check=false;
        }
        if (util.isNotEmpty(labelList)) {
          labelList.forEach(x => {

            if (util.isNotEmpty(x.list)) {
              let show_value = "";
              if (util.isNotEmpty(x.copy_name)) {
                let r_value = element[x.copy_name];
                //  
                for (let a = 0; a < x.list.length; a++) {
                  let json = x.list[a];
                  if (json.value == r_value) {
                    show_value = json.label;
                    break;
                  }
                }
                element[x.name] = show_value;

              }
              if(x.edit_type=='select'){
                let r_value = element[x.name];
                for (let a = 0; a < x.list.length; a++) {
                  let json = x.list[a];
                  if (json.value == r_value) {
                    show_value = json.label;
                    break;
                  }
                }
                element[x.name+"_value"] = show_value;

              }

            } 
            //  }
          })
        }

      });
      // console.log("---table list:",list);
      let tableHeight = (list.length) * base_height;
      this.setData({
        tableHeight,
        dataList1: list,
        allcheck:false
        // select
        //  labelList1: this.props.labelList
        // tableWidth1
      })
    },
    onTextFn(e){
      let isnum = e.currentTarget.dataset.isnum;
      
      if(isnum){
        this.bindNumInput(e)
      }else{
        this.bindKeyInput(e)
      }
    },
    bindKeyInput(e) {
      let key = e.currentTarget.dataset.key;
      let index = e.currentTarget.dataset.index;
      let list = this.data.dataList1;
      let json = list[index];
      json[key] = e.detail.value;
      
      this.setData({
        [`dataList1[${index}]`]: json
        //   [`${dataList1}`]: e.detail.value,
      });
    },
    bindNumInput(e) {
      
      let that=this;
      let key = e.currentTarget.dataset.key;
      let index = e.currentTarget.dataset.index;
      let list = this.data.dataList1;
      let json = list[index];
      json[key] = e.detail.value;

      util.debounce(() => {
        if(!util.isPositiveInteger(e.detail.value)){
        //  util.message("请输入正整数!");
          let value=e.detail.value;
          
          if(value){
            value=Math.floor(parseFloat(value));
            json[key]="";
            that.setData({
              [`dataList1[${index}]`]: json,
            });
          }
          
          return;
        }else{
          that.setData({
            [`dataList1[${index}]`]: json,
          });
        }
        
       
      }, 300);
    }, 
    bindObjPickerChange(e) {
      let index = e.detail.value;
      let key = e.currentTarget.dataset.key;
      let $index = e.currentTarget.dataset.index;
      let attrindex = e.currentTarget.dataset.attrindex;
      let json = this.data.dataList1[$index];
      let labelJson = this.data.labelList1[attrindex].list[index];
      json[key] = labelJson.value;
      json[key + "_value"] = labelJson.label;
      // debugger
      this.setData({
        [`dataList1[${$index}]`]: json
      });
      // console.log(`---${$index}:`,this.data.dataList1[$index]);

    },
    checkError() {
      let labelList = this.data.labelList1;
      let list = this.data.dataList1;
      // debugger
      let error = ""
      for (let a = 0; a < labelList.length; a++) {
        let ajson = labelList[a];
        if (ajson.edit) {

          for (let b = 0; b < list.length; b++) {
            let bjson = list[b];
            if (util.isEmpty(bjson[ajson.name])) {
              // 
              error = `第${b + 1}行 请填写${ajson.text}`
              break;
            }
            if (ajson.is_num && parseFloat(bjson[ajson.name]) <= 0) {
              error = `第${b + 1}行 ${ajson.text}请输入大于0的数字`
              break;
            }
            if(ajson.checkNum&&ajson.is_num&&parseFloat(bjson[ajson.name]) >parseFloat(bjson.checkNum)){
              error = `第${b + 1}行 ${ajson.text} 不能${ajson.checkNumMsg}: ${bjson.checkNum}`
              break;
            }
          }
        }
      }
      return error;
    },
    scrollY(e) {
      this.setData({
        scrollTop: e.detail.scrollTop,
      });
    },
    scrollY2_1fn(e) {
      this.setData({
        scrollY2_2: e.detail.scrollTop,
      });
    },
    scrollX2_1(e) {
      this.setData({
        right: 0,
        left: 1,
        scrollX2_1: e.detail.scrollLeft,
      });

    },
    scrollX(e) {
      this.setData({
        scrollLeft: e.detail.scrollLeft,
      });
    },
    i_popover(ref) {
      this.i_popover = ref;
    },
    getLableList() {

    },
    getSelectValue(e) {
      return "123";
    },
    checkAllboxChange(e) {
      //allcheck
      //debugger
      let dataList = this.data.dataList1;
      if (e.detail.value) {
        dataList.forEach(element => {
          element.check = true;
        });
      } else {
        dataList.forEach(element => {
          element.check = false;
        });
      }
 //     console.log(`---dataList:`,dataList);
      this.setData({
        dataList1: dataList,
        allcheck: e.detail.value
      })
    },
    checkAllChange(){
      let allcheck=this.data.allcheck;
      let dataList = this.data.dataList1;

      allcheck=!allcheck;

      if (allcheck) {
        dataList.forEach(element => {
          element.check = true;
        });
      } else {
        dataList.forEach(element => {
          element.check = false;
        });
      }
      this.setData({
        allcheck,
        dataList1:dataList
      });
    },
    checkRowChange(e){
      let index = e.currentTarget.dataset.index;
      let dataList = this.data.dataList1;
      let json=dataList[index];
      json.check=!json.check;
      if (json.check) {
        // 
        if (this.data.select == "single") {
          dataList.forEach(element => {
            element.check = false;
          });
          this.setData({
            dataList1: dataList
          })
        }
        //   dataList[index].check=true;
        this.setData({
          [`dataList1[${index}].$index`]: index,
          [`dataList1[${index}].check`]: true
        })
//        console.log(this.data.dataList1[index]);
      } else {
        // dataList[index].check=false;
        this.setData({
          [`dataList1[${index}].$index`]: index,
          [`dataList1[${index}].check`]: false
        })
      }
    },
    checkboxChange(e) {
      let index = e.currentTarget.dataset.index;
      let dataList = this.data.dataList1;
 //     debugger
      if (e.detail.value) {
        // 
        if (this.data.select == "single") {
          dataList.forEach(element => {
            element.check = false;
          });
          this.setData({
            dataList1: dataList
          })
        }
        //   dataList[index].check=true;
        this.setData({
          [`dataList1[${index}].$index`]: index,
          [`dataList1[${index}].check`]: true
        })
        console.log(this.data.dataList1[index]);
      } else {
        // dataList[index].check=false;
        this.setData({
          [`dataList1[${index}].$index`]: index,
          [`dataList1[${index}].check`]: false
        })
      }
      // this.setData({
      //   dataList1:dataList
      // })
      //  console.log(this.data.dataList1[`${index}`].check);
    },
    getCheckList() {
      let checklist = [];
      let list = this.data.dataList1;
      checklist = list.filter(element => {
        return element.check
      });
      // if(util.isEmpty(checklist)){
      //   util.message("请先勾选数据!")
      //   return false;
      // }
      // if(this.data.select=="single"&&checklist.length>1){
      //   util.message("请只勾选一条数据!")
      //   return false;
      // }
      return checklist;
    },
    resetCheckList() {
      let list = this.data.dataList1;
      list.forEach(element => {
        element.check = false;
      });
      this.setData({
        dataList1: list
      })
      console.log(list[0]);
    },
    openPopover(e) {
      let index = e.currentTarget.dataset.index;
      let pindex = e.currentTarget.dataset.pindex;
      let obj = this.data.dataList1[pindex];
      console.log(obj);

      this.i_popover.openFn(obj);
    }
  },
});
